package com.dabai.qrtools.other;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;

import com.dabai.qrtools.utils.DabaiUtils;
import com.dabai.qrtools.utils.StringUtils;

/**
 * Description : Other
 *
 * @author BAI
 */
public class ContactUtils {


    @SuppressLint("Range")
    public static String[] getPhoneContacts(Context context,Uri uri) {
        String[] contact = new String[2];
        //得到ContentResolver对象
        ContentResolver cr = context.getContentResolver();
        //取得电话本中开始一项的光标
        Cursor cursor = cr.query(uri, null, null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
            //取得联系人姓名
            int nameFieldColumnIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
            contact[0] = cursor.getString(nameFieldColumnIndex);
            //取得电话号码
            String ContactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactId, null, null);
            if (phone != null) {

                phone.moveToFirst();
                contact[1] = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            }
            phone.close();
            cursor.close();
        } else {
            return null;
        }
        return contact;
    }


    /**
     * 创建新的联系人
     *
     * @param context 上下文
     * @param name    的名字
     * @param phone   电话
     */
    public static void createNewContact(Context context,String name, String phone) {
        try {
            Intent intent = new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra(ContactsContract.Intents.Insert.NAME, name);
            intent.putExtra(ContactsContract.Intents.Insert.PHONE, phone);
            context.startActivity(intent);
        } catch (Exception e) {
            DabaiUtils.showToast(context,"失败");
        }
    }

    /**
     * 保存存在联系
     *
     * @param context 上下文
     * @param name    的名字
     * @param phone   电话
     */
    public static void saveExistContact(Context context,String name, String phone) {
        try {
            Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setType("vnd.android.cursor.item/person");
            intent.setType("vnd.android.cursor.item/contact");
            intent.setType("vnd.android.cursor.item/raw_contact");
            //    intent.putExtra(android.provider.ContactsContract.Intents.Insert.NAME, name);
            intent.putExtra(ContactsContract.Intents.Insert.PHONE, phone);
            intent.putExtra(ContactsContract.Intents.Insert.PHONE_TYPE, 3);
            context.startActivity(intent);
        } catch (Exception e) {
            DabaiUtils.showToast(context,"失败");
        }
    }


    public static String getName(String content){

        String[] people = content.split("\n");

        String name = "";

        for (String line : people) {
            String[] tmp = line.split(":");
            String key = tmp[0];
            String value = tmp[1];

            if (key.equalsIgnoreCase("FN")) {
                name = value;
            }
        }
        return name.replaceAll(";","");
    }

    public static String getPhoneNum(String content){
        String[] people = content.split("\n");

        String phoneNum = "";

        for (String line : people) {
            String[] tmp = line.split(":");
            String key = tmp[0];
            String value = tmp[1];

            if (StringUtils.containsIgnoreCase(key,"TEL")) {
                phoneNum = value;
            }
        }
        return phoneNum.replaceAll(";","");
    }


}
